// ==UserScript==
// @name         大屏页面控制
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        http://127.0.0.1:2080/2021/page1/index.html
// @match        http://127.0.0.1:2080/2021/page1/index.html?theme=234
// @match        http://127.0.0.1:2080/2021/page2/iframe.html*
// @grant        none
// @run-at       document-idle
// ==/UserScript==

(function() {
    'use strict';
        console.log('rainbow script injected')
        var autoReconnect = true;
        var wsUrl = "127.0.0.1:10106/ws/page/test";
        var websocket = null;
        var page1url = "http://127.0.0.1:2080/2021/page1/index.html";
        var page2url = "http://127.0.0.1:2080/2021/page1/index.html?theme=234";
        var frameurl = "http://127.0.0.1:2080/2021/page2/iframe.html";
        var currentPage = window.location.href;
        createWebSocketConnection(wsUrl)
        function createWebSocketConnection() {
            if('WebSocket' in window){
                connect('ws://' + wsUrl)
            }
        }
        function clearTab1ThemeElement() {
            //关闭人民广场底座
            window.g.index2d.hideBigBase();
            //关闭三线三站底座
            window.g.index2d.hide3Station();
            //清除观车观站面板
            window.g.index2d.hideAllPanel();
        }
        function connect(host) {
            if (undefined === websocket || null === websocket) {
                console.log('websocket connect to host' + host)
                websocket = new WebSocket(host);
            }

            websocket.onopen = function () {
                console.log('websocket connection open...')
            };

            websocket.onmessage = function (event) {
                try {
                    console.log('receive message:' + event.data)
                    var received_msg = JSON.parse(event.data);
                    //page指定逻辑
                    if(received_msg.type == "page")
                    {
                        let commandStrs = received_msg.command.split(":")
                        let page = commandStrs[0] //page序号
                        let command = commandStrs[1] //具体指令
                        if(currentPage == page1url && page == "page1")
                        {
                            //处理page1指令
                            switch (command) {
                                case "seeStation":
                                    window.g.index2d.showStationPanel("0123", "人民广场");
                                    break;
                                case "seeTrain":
                                    window.g.index2d.showCarPanel("17", '1701', "17A01");
                                    break;
                                case "clearPanel":
                                    window.g.index2d.hideAllPanel();
                                    break;
                                case "changePanel":
                                    window.g.index2d.changePanel();
                                    break;
                                case "farToMid":
                                    window.g.index2d.farToMid();
                                    break;
                                case "midToNear":
                                    window.g.index2d.midToNear();
                                    break;
                                case "nearToFar":
                                    window.g.index2d.nearToFar();
                                    break;
                                case "lookInit":
                                    window.g.index2d.lookInit();
                                    break;
                                case "autoCamera":
                                    window.rstart();
                                    break;
                                case "stopCamera":
                                    window.rstop();
                                    break;
                                case "hidePeople":
                                    window.g.index2d.hidePeople();
                                    break;
                                case "showPeople":
                                    window.g.index2d.showPeople();
                                    break;
                                case "hideBigBase":
                                    window.g.index2d.hideBigBase();
                                    break;
                                case "showBigBase":
                                    window.g.index2d.showBigBase();
                                    break;
                                case "hide3Station":
                                    window.g.index2d.hide3Station();
                                    break;
                                case "show3Station":
                                    window.g.index2d.show3Station();
                                    break;
                                case "lookSquare":
                                    window.g.index2d.lookSquare();
                                    break;
                                case "look3Line3Station":
                                    window.g.index2d.look3Line3Station();
                                    break;
                                case "theme1Show":
                                    clearTab1ThemeElement();
                                    //切换到初始视角
                                    window.g.index2d.lookInit();
                                    //显示客流
                                    window.g.index2d.showPeople();
                                    //切换主题面板
                                    window.g.index2d.theme1Show();
                                    break;
                                case "theme2Show"://切换至人民广场视角，并展示相应主题面板
                                    clearTab1ThemeElement();
                                    //隐藏客流
                                    window.g.index2d.hidePeople();
                                    //展示底座
                                    window.g.index2d.showBigBase();
                                    //切换视角
                                    window.g.index2d.lookSquare();
                                    //切换主题面板
                                    window.g.index2d.theme2Show();
                                    break;
                                case "theme3Show"://切换至三线三站视角，并展示相应主题面板
                                    clearTab1ThemeElement();
                                    //隐藏客流
                                    window.g.index2d.hidePeople();
                                    //展示底座
                                    window.g.index2d.show3Station();
                                    //切换视角
                                    window.g.index2d.look3Line3Station();
                                    //切换主题面板
                                    window.g.index2d.theme3Show();
                                    break;
                                case "theme4Show":
                                    clearTab1ThemeElement();
                                    //切换到初始视角
                                    window.g.index2d.lookInit();
                                    //隐藏客流
                                    window.g.index2d.hidePeople();
                                    //切换左右面板
                                    //window.g.index2d.changePanel();
                                    //切换主题面板
                                    window.g.index2d.theme4Show();
                                    break;
                                case "theme5Show":
                                    clearTab1ThemeElement();
                                    //切换到初始视角
                                    window.g.index2d.lookInit();
                                    //隐藏客流
                                    window.g.index2d.hidePeople();
                                    //切换主题面板
                                    window.g.index2d.theme5Show();
                                    break;
                                case "jinbo11"://切换进博视角，并展示相应主题面板
                                    clearTab1ThemeElement();
                                    //隐藏客流
                                    window.g.index2d.hidePeople();
                                    //展示底座
                                    window.g.index2d.show3Station();
                                    //切换视角
                                    window.g.index2d.look3Line3Station();
                                    //切换主题面板
                                    window.g.index2d.jinbo11Show();
                                    break;
                                case "jinbo12"://切换进博视角，并展示相应主题面板
                                    clearTab1ThemeElement();
                                    //隐藏客流
                                    window.g.index2d.hidePeople();
                                    //展示底座
                                    window.g.index2d.show3Station();
                                    //切换视角
                                    window.g.index2d.look3Line3Station();
                                    //切换主题面板
                                    window.g.index2d.jinbo12Show();
                                    break;
                                case "showSign"://开启五大新城
                                    window.g.index2d.showSign();
                                    break;
                                case "hideSign"://关闭五大新城
                                    window.g.index2d.hideSign();
                                    break;
                                case "changePanel1"://主题1面板1
                                    window.g.index2d.changePanel1();
                                    break;
                                case "changePanel2"://主题1设备面板
                                    window.g.index2d.changePanel2();
                                    break;
                                case "changePanel3"://主题1面板3
                                    window.g.index2d.changePanel3();
                                    break;
                                case "showRedRoad"://主题1开启红线
                                    window.g.index2d.showRedRoad();
                                    break;
                                case "hideRedRoad"://主题1关闭红线
                                    window.g.index2d.hideRedRoad();
                                    break;
                                case "showNav1":
                                    window.g.index2d.showNav1();
                                    break;
                                case "showNav2":
                                    window.g.index2d.showNav2();
                                    break;
                                case "showNav3":
                                    window.g.index2d.showNav3();
                                    break;
                                case "showNav4":
                                    window.g.index2d.showNav4();
                                    break;
                                case "showNav5":
                                    window.g.index2d.showNav5();
                                    break;
                                case "showOperateMain1":
                                    window.g.index2d.showOperateMain1();
                                    break;
                                case "showOperateMain2":
                                    window.g.index2d.showOperateMain2();
                                    break;
                                case "showBenefit1":
                                    window.g.index2d.showBenefit1();
                                    break;
                                case "showBenefit2":
                                    window.g.index2d.showBenefit2();
                                    break;
                                case "showBenefit3":
                                    window.g.index2d.showBenefit3();
                                    break;
                                case "showBenefit4":
                                    window.g.index2d.showBenefit4();
                                    break;
                                case "showEquipment1":
                                    window.g.index2d.showEquipment1();
                                    break;
                                case "showEquipment2":
                                    window.g.index2d.showEquipment2();
                                    break;
                                case "showEquipment3":
                                    window.g.index2d.showEquipment3();
                                    break;
                                case "showDevelop1":
                                    window.g.index2d.showDevelop1();
                                    break;
                                case "showDevelop2":
                                    window.g.index2d.showDevelop2();
                                    break;
                                case "showManagement1":
                                    window.g.index2d.showManagement1();
                                    break;
                                case "showManagement2":
                                    window.g.index2d.showManagement2();
                                    break;
                                case "showHistory":
                                    window.g.index2d.showHistory();
                                    window.g.index2d.hideCar();
                                    window.g.index2d.hidePeople();
                                    break;
                                case "hideHistory":
                                    window.g.index2d.hideHistory();
                                    window.g.index2d.showCar();
                                    window.g.index2d.showPeople();
                                    break;
                                case "showFlyLine":
                                    window.g.index2d.showFlyLine();
                                    break;
                                case "hideFlyLine":
                                    window.g.index2d.hideFlyLine();
                                    break;
                                case "showFlowLine":
                                    window.g.index2d.showFlowLine();
                                    //window.g.index2d.showBigStation();
                                    break;
                                case "hideFlowLine":
                                    window.g.index2d.hideFlowLine();
                                    window.g.index2d.hideBigStation();
                                    break;
                                case "showBigStation":
                                    window.g.index2d.showBigStation();
                                    break;
                                case "hideBigStation":
                                    window.g.index2d.hideBigStation();
                                    break;
                                case "showHightLight":
                                    window.g.index2d.showHightLight();
                                    break;
                                case "hideHightLight":
                                    window.g.index2d.hideHightLight();
                                    break;
                                case "showHeatMap":
                                    window.g.index2d.hideCar();
                                    window.g.index2d.hidePeople();
                                    window.g.index2d.showHeatMap();
                                    break;
                                case "hideHeatMap":
                                    window.g.index2d.showCar();
                                    window.g.index2d.showPeople();
                                    window.g.index2d.hideHeatMap();
                                    break;
                                case "showMonitor1":
                                    window.g.index2d.showMonitor1();
                                    break;
								case "showMonitor2":
                                    window.g.index2d.showMonitor2();
                                    break;
                                case "showMonitor3":
                                    window.g.index2d.showMonitor3();
                                    break;
								case "showMonitor4":
                                    window.g.index2d.showMonitor4();
                                    break;
                                case "showMonitor5":
                                    window.g.index2d.showMonitor5();
                                    break;
								case "showMonitor6":
                                    window.g.index2d.showMonitor6();
                                    break;
                                case "hideMonitor":
                                    window.g.index2d.hideMonitor();
                                    break;
                                case "showCar":
                                    window.g.index2d.showCar();
                                    break;
                                case "hideCar":
                                    window.g.index2d.hideCar();
                                    break;
                                case "showIndexVideo":
                                    window.g.index2d.showIndexVideo();
                                    break;
                                case "hideIndexVideo":
                                    window.g.index2d.hideIndexVideo();
                                    break;
                                case "showTimeBox":
                                    window.g.index2d.showTimeBox();
                                    break;
                                case "hideTimeBox":
                                    window.g.index2d.hideTimeBox();
                                    break;
                                case "showPie":
                                    window.g.index2d.showPie();
                                    break;
                                case "hidePie":
                                    window.g.index2d.hidePie();
                                    break;
                                case "showLineDriverless":
                                    window.g.index2d.showLineDriverless();
                                    break;
                                case "hideLineDriverless":
                                    window.g.index2d.hideLineDriverless();
                                    break;
                                case "showEmergency":
                                    window.g.index2d.showEmergency();
                                    break;
                                case "hideEmergency":
                                    window.g.index2d.hideEmergency();
                                    break;
                                case "showDevelop3LineBg":
                                    window.g.index2d.showDevelop3LineBg();
                                    break;
                                case "showDevelop3Line2":
                                    window.g.index2d.showDevelop3Line2();
                                    break;
                                case "showDevelop3Line14":
                                    window.g.index2d.showDevelop3Line14();
                                    break;
                                case "showOperateMainPanel1":
                                    window.g.index2d.showOperateMainPanel1();
                                    break;
                                case "showOperateMainPanel2":
                                    window.g.index2d.showOperateMainPanel2();
                                    break;
                                case "showVideoNanjing":
                                    window.g.index2d.showVideoNanjing();
                                    break;
                                case "hideVideoNanjing":
                                    window.g.index2d.hideVideoNanjing();
                                    break;
                                case "showThreePanel":
                                    window.g.index2d.showThreePanel();
                                    break;
                                case "hideThreePanel":
                                    window.g.index2d.hideThreePanel();
                                    break;
                                case "showFlowIframe":
                                    window.g.index2d.showFlowIframe();
                                    break;
                                case "hideFlowIframe":
                                    window.g.index2d.hideFlowIframe();
                                    break;
                                case "showJinbohui":
                                    window.g.index2d.showJinbohui();
                                    break;
                                case "hideJinbohui":
                                    window.g.index2d.hideJinbohui();
                                    break;
                                case "showDaduhui":
                                    window.g.index2d.showDaduhui();
                                    break;
                                case "hideDaduhui":
                                    window.g.index2d.hideDaduhui();
                                    break;        
                                case "showHulianhutong":
                                    window.g.index2d.showHulianhutong();
                                    break;
                                case "hideHulianhutong":
                                    window.g.index2d.hideHulianhutong();
                                    break;
                                case "showBIM":
                                    window.g.index2d.showBIM();
                                    break;
                                case "hideBIM":
                                    window.g.index2d.hideBIM();
                                    break;
                                case "showSiteDensity":
                                    window.g.index2d.showSiteDensity(true);
                                    break;
                                case "hideSiteDensity":
                                    window.g.index2d.showSiteDensity(false);
                                    break;
                                case "showMaxFlow":
                                    window.g.index2d.showMaxFlow(true);
                                    break;
                                case "hideMaxFlow":
                                    window.g.index2d.showMaxFlow(false);
                                    break;
                                case "showPhotovoltaicBase":
                                    window.g.index2d.showPhotovoltaicBase(true);
                                    break;
                                case "hidePhotovoltaicBase":
                                    window.g.index2d.showPhotovoltaicBase(false);
                                    break;
                                case "showShare":
                                    window.g.index2d.showShare();
                                    break;
                                case "showSatisfaction":
                                    window.g.index2d.showSatisfaction();
                                    break;
                                case "showMCBF":
                                    window.g.index2d.showMCBF(true);
                                    break;
                                case "hideMCBF":
                                    window.g.index2d.showMCBF(false);
                                    break;
                                case "showVehicle":
                                    window.g.index2d.showVehicle(true);
                                    break;
                                case "hideVehicle":
                                    window.g.index2d.showVehicle(false);
                                    break;
                                case "showgdbdzb":
                                    window.g.index2d.showgdbdzb(true);
                                    break;
                                case "hidegdbdzb":
                                    window.g.index2d.showgdbdzb(false);
                                    break;
                                case "showBusinessRight1":
                                    window.g.index2d.showBusinessRight1();
                                    break;
                                case "showBusinessRight2":
                                    window.g.index2d.showBusinessRight2();
                                    break;
                                case "showSiteInfo":
                                    window.g.index2d.showSiteInfo(true);
                                    break;
                                case "hideSiteInfo":
                                    window.g.index2d.showSiteInfo(false);
                                    break;
                                case "showSmartVideo":
                                    window.g.index2d.showSmartVideo();
                                    break;
                                case "hideSmartVideo":
                                    window.g.index2d.hideSmartVideo();
                                    break;
                                case "showTopDate1":
                                    window.g.index2d.showTopDate(1);
                                    break;
                                case "showTopDate2":
                                    window.g.index2d.showTopDate(2);
                                    break;
                                case "showInnovateNum0":
                                    window.g.index2d.showInnovateNum(0);
                                    break;
                                case "showInnovateNum1":
                                    window.g.index2d.showInnovateNum(1);
                                    break;
                                case "showInnovateNum2":
                                    window.g.index2d.showInnovateNum(2);
                                    break;
                                case "showInnovateNum3":
                                    window.g.index2d.showInnovateNum(3);
                                    break;
                                case "showInnovateNum4":
                                    window.g.index2d.showInnovateNum(4);
                                    break;
                                case "showInnovateNum5":
                                    window.g.index2d.showInnovateNum(5);
                                    break;
                                case "showInnovateNum6":
                                    window.g.index2d.showInnovateNum(6);
                                    break;
                                case "showInnovateNum7":
                                    window.g.index2d.showInnovateNum(7);
                                    break;
                                case "show14Station1":
                                    window.g.index2d.show14Station(true);
                                    break;
                                case "show14Station2":
                                    window.g.index2d.show14Station(false);
                                    break;
                                case "showAverage1":
                                    window.g.index2d.showAverage(true);
                                    break;
                                case "showAverage2":
                                    window.g.index2d.showAverage(false);
                                case "showVideoWaitan":
                                    window.g.index2d.showVideoWaitan();
                                    break;
                                case "hideVideoWaitan":
                                    window.g.index2d.hideVideoWaitan();
                                    break;
                                case "showgCarriage":
                                    window.g.index2d.showgCarriage(true);
                                    break;
                                case "hideCarriage":
                                    window.g.index2d.showgCarriage(false);
                                    break;
                                case "reloadPage":
                                    location.reload();
                                    break;
                                default:
                                    break;
                            }
                        }else if(currentPage == page2url && page == "page2")
                        {
                            //处理page2指令
                            let t3video = document.getElementById('theme3Video');
                            switch (command) {
                                case "t4to145":
                                    window.g.theme234.showThemeTab1();
                                    break;
                                case "t4toDataCenter":
                                    window.g.theme234.showThemeTab2();
                                    break;
                                case "t3next":
                                    window.g.theme234.theme3Change('next');
                                    break;
                                case "t3pre":
                                    window.g.theme234.theme3Change('pre');
                                    break;
                                case "t2mopes":
                                    window.g.theme234.showTheme2Tab1();
                                    break;
                                case "t2data":
                                    window.g.theme234.showTheme2Tab2();
                                    break;
                                case "t2pressure":
                                    window.g.theme234.showTheme2Tab3();
                                    break;
                                case "theme2show1":
                                    window.g.theme234.theme2show1();
                                    break;
                                case "theme2show2":
                                    window.g.theme234.theme2show2();
                                    break;
                                case "t3muted":
                                    t3video.muted = true;
                                    break;
                                case "t3cancelMuted":
                                    t3video.muted = false;
                                    break;
                                case "t3pauseVideo":
                                    t3video.pause();
                                    break;
                                case "t3playVideo":
                                    t3video.play();
                                    break;
                                case "t4theme1":
                                    window.g.theme234.showTheme4tab1();
                                    break;
                                case "t4theme2":
                                    window.g.theme234.showTheme4tab2();
                                    break;
                                case "t4theme3":
                                    window.g.theme234.showTheme4tab3();
                                    break;
                                case "t4theme4":
                                    window.g.theme234.showTheme4tab4();
                                    break;
                                case "t4theme5":
                                    window.g.theme234.showTheme4tab5();
                                    break;
                                case "t4theme6":
                                    window.g.theme234.showTheme4tab6();
                                    break;
                                default:
                                    break;
                            }
                        }else{
                            console.log("page match failed")
                            //do nothing
                        }
                    }else if(received_msg.type == "frame")
                    {
                        let frameNode = document.getElementById('frame');
                        let commandStrs = received_msg.command.split(":")
                        let frameName = commandStrs[0] //frame名
                        let command = commandStrs[1] //具体指令
                        let currentFrame = GetQueryString("name");
                        if(frameName == currentFrame)
                        {
                            //url模式
                            if(command.indexOf('urlmode') > -1)
                            {
                                let url = received_msg.command.replace(commandStrs[0]+':urlmode:','')
                                switchFrameUrl(frameNode,url)
                            }
                            else{
                                switch (command) {
                                    case "car1":
                                        switchFrameUrl(frameNode,"http://172.20.50.11/static/shmetro-rise-ui/#/")
                                        //frameNode.src = "http://172.20.50.11/static/shmetro-rise-ui/#/";
                                        break;
                                    case "car2":
                                        switchFrameUrl(frameNode,"http://172.20.50.11/shior/shior/templates/main.html")
                                        //frameNode.src = "http://172.20.50.11/shior/shior/templates/main.html";
                                        break;
                                    case "electric1":
                                        switchFrameUrl(frameNode,"http://192.168.200.12/iomm/dashboard/runninginterfaces/index")
                                        //frameNode.src = "http://192.168.200.12/iomm/dashboard/runninginterfaces/index";
                                        break;
                                    case "electric2":
                                        switchFrameUrl(frameNode,"http://192.168.200.12/iomm/dashboard/lightplates/standardcurves/index")
                                        //frameNode.src = "http://192.168.200.12/iomm/dashboard/lightplates/standardcurves/index";
                                        break;
                                    case "seecar"://一键观车
                                        switchFrameUrl(frameNode,"http://172.20.50.11/shior/shior/templates/show.html?page=train&line=17&train=1701&type=17A01&secret=64706C436458446159504E64325A5473676D48674C6961494E5635363136386F6C5558517233682B4466503268776D636D746F4A7675357A3478426774646532556B43636C6745614E54787579647A366832736448513D3D")
                                        //frameNode.src = "http://172.20.50.11/shior/shior/templates/show.html?page=train&line=17&train=1701&type=17A01&secret=64706C436458446159504E64325A5473676D48674C6961494E5635363136386F6C5558517233682B4466503268776D636D746F4A7675357A3478426774646532556B43636C6745614E54787579647A366832736448513D3D";
                                        break;
                                    default:
                                        break;
                                }
                            }
                        }
                    }

                } catch (error) {
                    console.log(error)
                }
            };

            //If the websocket is closed but the session is still active, create new connection again
            websocket.onclose = function () {
                websocket = undefined;
                if (autoReconnect) {
                    createWebSocketConnection();
                }
            };
        }

        function switchFrameUrl(frameNode,url)
        {
            if(frameNode.src == url)
            {
                //do nothing
            }else{
                frameNode.src = url;
            }
        }

        function GetQueryString(name) { 
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 
            var r = window.location.search.substr(1).match(reg);  //获取url中"?"符后的字符串并正则匹配
            var context = ""; 
            if (r != null) 
                 context = r[2]; 
            reg = null; 
            r = null; 
            return context == null || context == "" || context == "undefined" ? "" : context; 
        }
})();
var rtimer1;
var rtimer2;
var rstart = function () {
    _farToMid();
}

function _farToMid()
{
    window.g.index2d.farToMid();
    rtimer1 = setTimeout(() => {
        _midToNear();
    }, 20000);
}

function _midToNear() {
    window.g.index2d.midToNear();
    rtimer2 = setTimeout(() => {
        _farToMid();
    }, 20000);
}

var rstop = function () {
    clearTimeout(rtimer1);
    clearTimeout(rtimer2);
}

window.rstart = rstart;
window.rstop = rstop;